import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

export const constantRouterMap = [
  // 上来的默认路由,社区主页
  {
    path: "/",
    redirect: "/community/article",
  },
  // 搜索路由
  {
    path: "/search",
    component: () => import("@/views/Search"),
    children: [
      {
        path: "article",
        component: () => import("@/views/Search/Article"),
        meta: { showNavBar: true, title: "搜索" },
      },
      {
        path: "user",
        component: () => import("@/views/Search/User"),
        meta: { showNavBar: true, title: "搜索" },
      },
    ],
    redirect: "/search/article",
  },
  // 社区路由
  {
    path: "/community",
    component: () => import("@/views/Community"),
    children: [
      {
        path: "article",
        component: () => import("@/views/Community/Article"),
        meta: { showNavBar: true, title: "社区" },
      },
      {
        path: "detail/:id",
        name: "Detail",
        component: () => import("@/views/Community/Detail"),
        meta: { showNavBar: true, title: "详情" },
      },
    ],
    redirect: "/community/article",
  },
  // 发布主题文章内容
  {
    path: "/markdown",
    component: () => import("@/views/Markdown"),
    children: [
      {
        path: "editor",
        component: () => import("@/views/Markdown/Editor"),
        meta: { showNavBar: false, title: "编写文章" },
      },
    ],
  },
  // 登录注册页
  {
    path: "/user",
    component: () => import("@/views/User"),
    children: [
      {
        path: "register",
        component: () => import("@/views/User/Register"),
        meta: { showNavBar: true, title: "注册" },
      },
      {
        path: "login",
        component: () => import("@/views/User/Login"),
        meta: { showNavBar: true, title: "登录" },
      },
      // 移动端登录
      {
        path: "mobileLogin",
        component: () => import("@/views/User/MobileLogin"),
        meta: { showNavBar: false, title: "登录" },
      },
      // 移动端注册
      {
        path: "mobileRegister",
        component: () => import("@/views/User/MobileRegister"),
        meta: { showNavBar: false, title: "注册" },
      },
    ],
    // redirect: "/user/login",
  },
  // 个人中心路由
  {
    path: "/center/:id",
    component: () => import("@/views/Center"),
    children: [
      {
        path: "info",
        component: () => import("@/views/Center/Info"),
        meta: { showNavBar: true, title: "信息概况" },
      },
      {
        path: "material",
        component: () => import("@/views/Center/Material"),
        meta: { showNavBar: true, title: "个人资料" },
      },
      {
        path: "article",
        component: () => import("@/views/Center/Article"),
        meta: { showNavBar: true, title: "相关文章" },
      },
      {
        path: "concern",
        component: () => import("@/views/Center/Concern"),
        meta: { showNavBar: true, title: "我的关注" },
      },
      {
        path: "fans",
        component: () => import("@/views/Center/Fans"),
        meta: { showNavBar: true, title: "我的粉丝" },
      },
      {
        path: "collect",
        component: () => import("@/views/Center/Collect"),
        meta: { showNavBar: true, title: "我的收藏" },
      },
      {
        path: "history",
        component: () => import("@/views/Center/History"),
        meta: { showNavBar: true, title: "浏览历史" },
      },
    ],
    redirect: "/center/:id/info",
  },
  // 杂页，qq群之类无关紧要的
  {
    path: "/sundry",
    component: () => import("@/views/User"),
    children: [
      {
        path: "flock",
        component: () => import("@/views/Sundry/Flock"),
        meta: { showNavBar: true },
      },
    ],
    // redirect: "/sundry/login", //路由重定向,进入user定向到login
  },
  // 默认结果页面
  {
    path: "/default",
    component: () => import("@/views/Default"),
    children: [
      {
        path: "500",
        component: () => import("@/views/Default/500"),
        meta: { showNavBar: false },
      },
      {
        path: "404",
        component: () => import("@/views/Default/404"),
        meta: { showNavBar: false },
      },
      {
        path: "403",
        component: () => import("@/views/Default/403"),
        meta: { showNavBar: false },
      },
    ],
    redirect: "/community", //路由重定向,进入community定向到article
  },
  // 没有的页面全部跳转到 404 页面
  {
    path: "*",
    redirect: "/default/404", // 跳转到404页面
  },
];

// 处理编程式导航多次点击出现的抛错行为
let originPush = VueRouter.prototype.push;
let originRelpace = VueRouter.prototype.replace;
VueRouter.prototype.push = function (location) {
  return originPush.call(this, location).catch((err) => err);
};
VueRouter.prototype.replace = function (location) {
  return originRelpace.call(this, location).catch((err) => err);
};

export default new VueRouter({
  mode: "hash",
  // mode: "history",
  scrollBehavior(thisRouter) {
    if (/\/center/.test(thisRouter.fullPath)) return;
    return { x: 0, y: 0 };
  },
  routes: constantRouterMap,
});
